package org.yuangu.privacyfinance.activities.viewmodel;

import android.app.Application;

import com.privacy.accountmgr.R;

import org.yuangu.privacyfinance.domain.FinanceDatabase;
import org.yuangu.privacyfinance.domain.access.CategoryDao;
import org.yuangu.privacyfinance.domain.model.Category;
import org.yuangu.privacyfinance.domain.model.Transaction;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

public class CategoryViewModel extends TransactionListViewModel {
    private CategoryDao categoryDao = FinanceDatabase.getInstance().categoryDao();
    private long categoryId;
    private LiveData<Category> category;
    private LiveData<Long> categoryBalanceMonth;
    private LiveData<Long> categoryIncomeMonth;
    private LiveData<Long> categoryExpensesMonth;
    private LiveData<List<Transaction>> transactions;

    public CategoryViewModel(@NonNull Application application, long categoryId) {
        super(application);
        setNavigationDrawerId(R.id.nav_category);
        this.categoryId = categoryId;
        category = categoryDao.get(categoryId);
        categoryBalanceMonth = FinanceDatabase.getInstance().transactionDao().sumForCategoryThisMonth(categoryId);
        categoryIncomeMonth = FinanceDatabase.getInstance().transactionDao().sumIncomeForCategoryThisMonth(categoryId);
        categoryExpensesMonth = FinanceDatabase.getInstance().transactionDao().sumExpensesForCategoryThisMonth(categoryId);
        setNavigationDrawerId(R.id.nav_category);
        setPreselectedCategoryId(categoryId);
        setShowEditMenu(true);
    }

    @Override
    public boolean showDrawer() {
        return false;
    }

    public LiveData<Long> getCategoryIncomeMonth() {
        return categoryIncomeMonth;
    }

    public LiveData<Long> getCategoryExpensesMonth() {
        return categoryExpensesMonth;
    }

    public LiveData<Long> getCategoryBalanceMonth() {
        return categoryBalanceMonth;
    }

    public LiveData<Category> getCategory() {
        return category;
    }

    @Override
    protected LiveData<List<Transaction>> fetchTransactions() {
        return transactionDao.getForCategory(categoryId);
    }

    public static class CategoryViewModelFactory implements ViewModelProvider.Factory {
        private Application application;
        private long categoryId;

        public CategoryViewModelFactory(Application application, long categoryId) {
            this.application = application;
            this.categoryId = categoryId;
        }

        @Override
        public <T extends ViewModel> T create(Class<T> modelClass) {
            return (T) new CategoryViewModel(application, categoryId);
        }
    }

    public CategoryDao getCategoryDao() {
        return categoryDao;
    }

    public void setCategoryDao(CategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }

    public long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(long categoryId) {
        this.categoryId = categoryId;
    }

    public void setCategory(LiveData<Category> category) {
        this.category = category;
    }

    public void setCategoryBalanceMonth(LiveData<Long> categoryBalanceMonth) {
        this.categoryBalanceMonth = categoryBalanceMonth;
    }

    public void setCategoryIncomeMonth(LiveData<Long> categoryIncomeMonth) {
        this.categoryIncomeMonth = categoryIncomeMonth;
    }

    public void setCategoryExpensesMonth(LiveData<Long> categoryExpensesMonth) {
        this.categoryExpensesMonth = categoryExpensesMonth;
    }

    @Override
    public LiveData<List<Transaction>> getTransactions() {
        return transactions;
    }

    public void setTransactions(LiveData<List<Transaction>> transactions) {
        this.transactions = transactions;
    }
}

